package com.meida.student.app.user.handler;

import com.meida.common.base.entity.EntityMap;
import com.meida.common.mybatis.model.PageParams;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.security.OpenHelper;
import com.meida.module.marketing.client.entity.MkgCouponUser;
import com.meida.module.marketing.provider.handler.UserCouponListByProductHandler;
import com.meida.module.marketing.provider.service.CouponUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * 用户优惠券列表
 *
 * @author zyf
 */
@Component
public class UserCouponListHandler extends UserCouponListByProductHandler {

    @Autowired
    private CouponUserService couponUserService;

    @Override
    public ResultBody validate(CriteriaQuery cq, EntityMap params) {
        return ResultBody.ok();
    }

    @Override
    public void prepare(CriteriaQuery<MkgCouponUser> cq, PageParams pageParams, EntityMap params) {
        super.prepare(cq, pageParams, params);

    }

    @Override
    public void complete(CriteriaQuery<MkgCouponUser> cq, List<EntityMap> result, EntityMap extra) {
        Long userId = OpenHelper.getUserId();
        Integer orderProductId = cq.getInt("productId", null);
        BigDecimal amount = new BigDecimal(cq.getParams("amount", "0"));
        //不可优惠券用数量
        Integer noUseCount = couponUserService.countDisabledCoupon(userId, orderProductId, amount);
        extra.put("noUseCount", noUseCount);
        //可用优惠券数量
        Integer couponCanUseNum = couponUserService.countEnabledCoupon(userId, orderProductId, amount);
        extra.put("couponCanUseNum", couponCanUseNum);

        for (EntityMap entityMap : result) {
            String couponTypeName = "";
            Integer productId = entityMap.getInt("productId", 1);
            if (productId.equals(0)) {
                couponTypeName = "通用券";
            }
            if (productId.equals(1)) {
                couponTypeName = "拍照提问";
            }
            if (productId.equals(2)) {
                couponTypeName = "预约辅导";
            }
            if (productId.equals(3)) {
                couponTypeName = "作业无忧";
            }
            entityMap.put("couponTypeName", couponTypeName);
        }
    }

}
